#include <math.h>	/* for Math functions */
#include <errno.h>	/* for errno, EDOM and ERANGE */
#include "hoc.h"	/* for execerror */

/* Prototype Functions */
double errcheck(double, const char *);

/* Error Checking functions */
double Log(double dx) {
	return errcheck(log(dx), "log");
}

double Log10(double dx) {
	return errcheck(log10(dx), "log10");
}

double Exp(double dx) {
	return errcheck(exp(dx), "exp");
}

double Sqrt(double dx) {
	return errcheck(sqrt(dx), "sqrt");
}

double Pow(double dx, double dy) {
	return errcheck(pow(dx,dy), "exponentiation");
}

double integer(double dx) {
	return (double)(long)dx;
}

/* Check result of library call */
double errcheck(double dx, const char* ptr_cstr) {
	if ( errno == EDOM ) {
		errno = 0;
		execerror(ptr_cstr, "argument out of domain");
	} else if ( errno == ERANGE ) {
		errno = 0;
		execerror(ptr_cstr, "result out of range");
	}
	return dx;
}
